//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <fstream>

#include "libraries.h"
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHLINE 100000
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool SplitColumn(char* buffer, TListE<String>* columns, const char* characters) {

  if (!buffer || !columns || !characters) return false;

  char *part = strtok(buffer, characters);

  while (part) {
    String* cell = new String(part);
    cell->Trim();
    columns->Add(cell);
    part = strtok(NULL, characters);
  }

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int main(int argc, char** argv) {

  String* filenameClusterIn;
  String* filenameISMIIn;
  String* filenameClusterOut;

  if (argc < 4) {
    cout << "Error in the input arguments. The execution must be: " << endl;
    cout << "./ClusterChangeNumberToIDE <Input cluster file> <Input Inter SMI file> <Output cluster file>" << endl;
    return 0;
  }
  else {
    cout << "---------------------------------------------------------------------------------" << endl;
    cout << " Input Cluster File : " << argv[1] << endl;
    cout << "   Ouput I SMI File : " << argv[2] << endl;
    cout << " Ouput Cluster File : " << argv[3] << endl;
    cout << "---------------------------------------------------------------------------------" << endl;
    filenameClusterIn  = new String(argv[1]);
    filenameISMIIn     = new String(argv[2]);
    filenameClusterOut = new String(argv[3]);
  }

  fstream fileClusterIn, fileClusterOut, fileISMIIn;

  fileClusterIn.open(filenameClusterIn->Out(),   fstream::in);
  fileISMIIn.open(filenameISMIIn->Out(),         fstream::in);
  fileClusterOut.open(filenameClusterOut->Out(), fstream::out);

  char             buffer[LENGTHLINE + 1];
  TListE <String>* listIDE    = new TListE <String> ();
  TListE <String>* listNumber = new TListE <String> ();
  TListE <String>* parts      = new TListE <String> ();

  cout << "Reading the Cluster File..." << endl;

  while (!fileISMIIn.eof()) {

    fileISMIIn.getline(buffer, LENGTHLINE);
    if (strlen(buffer) < 4) continue;
    parts->Clear();
    SplitColumn(buffer, parts, " ");

    String* ide    = new String(parts->Get(1));
    String* number = new String(parts->Get(2));

    listIDE->Add(ide);
    listNumber->Add(number);

  }

  fileISMIIn.close();

  cout << "Creating the Cluster File with IDE..." << endl;

  while (!fileClusterIn.eof()) {

    fileClusterIn.getline(buffer, LENGTHLINE);
    if (strlen(buffer) < 4) continue;
    parts->Clear();
    SplitColumn(buffer, parts, " ");

    parts->SetInitial();
    while (parts->SetNext())
      if (atoi(parts->GetCurrent()->Out()) == atoi(listNumber->Get(atoi(parts->GetCurrent()->Out()) - 1)->Out()))
        fileClusterOut << listIDE->Get(atoi(parts->GetCurrent()->Out()) - 1)->Out() << "\t";
      else
        cout << "Error: " << parts->GetCurrent()->Out();
    fileClusterOut << endl;
    
  }

  fileClusterIn.close();
  fileClusterOut.close();

  if (listIDE)            delete listIDE;
  if (listNumber)         delete listNumber;
  if (parts)              delete parts;

  if (filenameClusterIn)  delete filenameClusterIn;
  if (filenameISMIIn)     delete filenameISMIIn;
  if (filenameClusterOut) delete filenameClusterOut;

  return 1;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//




